package com.biglead.mini.controller;

import com.biglead.mini.thread.TestThreadPoolManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Queue;
import java.util.UUID;

@RestController
@RequestMapping("/test/order")
@Slf4j
public class TestOrderController {
    @Autowired
    TestThreadPoolManager testThreadPoolManager;

    /**
     * 测试模拟下单请求 入口
     *
     * @param id
     * @return
     */
    @GetMapping("/start/{id}")
    public String start(@PathVariable Long id) {
        //模拟的随机数
        String orderNo = System.currentTimeMillis() + UUID.randomUUID().toString();

        testThreadPoolManager.addOrders(orderNo,id);

        return "Test ThreadPoolExecutor start";
    }

    /**
     * 停止服务
     *
     * @param id
     * @return
     */
    @GetMapping("/end/{id}")
    public String end(@PathVariable Long id) {

        testThreadPoolManager.shutdown();

        Queue q = testThreadPoolManager.getMsgQueue();
        System.out.println("关闭了线程服务，还有未处理的信息条数：" + q.size());
        return "Test ThreadPoolExecutor start";
    }

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/addStock/{id}/{stock}")
    public String addRedis(@PathVariable Long id, @PathVariable Long stock) {

        log.info("向redis中添加商品 id {} 库存 {}", id, stock);
        redisTemplate.opsForValue().set("goodStock:" + id, stock);
        return "添加成功";
    }

}
